<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->ground_id = $request->getParameter('ground_id');
		if ($this->ground_id) {
			$this->ground = Doctrine_Core::getTable('DtGround')->find($this->ground_id);
			if ($this->ground)
				$this->ground_form = new AdminGroundForm($this->ground);
		}
		
		if ($request->hasParameter('ground')) {
			$form = $request->getParameter('ground');
			
			$this->status = $this->updateGroundFromRequest($this->ground, $form);
			try {
				$files = $this->getRequest()->getFiles();
				$file = $files['ground']['picture'];
				$tmpName = $file['tmp_name'];
				$fileName = $file['name'];
				$fileExt = trim(strrchr($fileName,'.'));

				$img = new sfImage($tmpName, $file['type']);
				$img->saveAs(sfConfig::get('sf_upload_dir').'/ground/'.$fileName, $file['type']);
				
				$img1 = new sfImage(sfConfig::get('sf_upload_dir').'/ground/'.$fileName, $file['type']);
				$fileName1 = ($this->ground->id).$fileExt;
				$img1->thumbnail(357,267,'center');
				$img1->setQuality(100);
				$img1->saveAs(sfConfig::get('sf_upload_dir').'/ground/'.$fileName1, $file['type']);
				
				$img1 = new sfImage(sfConfig::get('sf_upload_dir').'/ground/'.$fileName, $file['type']);
				$fileName1 = "doc_".$fileName;
				$img1->thumbnail(267,357,'center');
				$img1->setQuality(100);
				$img1->saveAs(sfConfig::get('sf_upload_dir').'/ground/'.$fileName1, $file['type']);
				
				$this->ground->picture = $fileName;
				$this->ground->save();
				$this->upload_success = true;				
			} catch (sfException $e) {
				//echo $e->getMessage();
			}
			
			if ($this->status) {
				$this->ground->save();
				$this->forward('ground', 'index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateGroundFromRequest(&$ground, $form)
	{
		if ($form['name']) {
			$c = Doctrine_Core::getTable('DtGround')->findOneByName($form['name']);
			if (!$c || ($c->name == $ground->name)) {
				$ground->name = $form['name'];
			} else {
				return false;
			}
		}
		
		if ($form['display_position'])
			$ground->display_position = $form['display_position'];
		if ($form['description'])
			$ground->description = $form['description'];

		$ground->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>